package com.fit.pages;

import java.util.Collection;
import java.util.Date;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.connection.UserSuppliedConnectionProvider;

import com.fit.beans.BorrowBean;
import com.fit.beans.CarBean;
import com.fit.beans.UserBean;
import com.fit.interfaces.BorrowI;
import com.fit.interfaces.CarI;
import com.fit.interfaces.User;
import com.fit.service.BorrowsI;
import com.fit.service.CarsI;

import com.fit.service.Users;
import com.fit.services.AppModule;
@SuppressWarnings("unused")
public class AddBorrow {

	
	@SessionState
	private User user;
	
	private boolean userExists;
	@Persist
	@Property
	private BorrowI chosenBorrow;
	
	@Persist
	private long borrowCar_id;

	@Persist
	private String borrowcar_modelName;
	
	@Persist
	private double price;


	public long getBorrowCar_id() {
		return borrowCar_id;
	}

	public void setBorrowCar_id(long borrowCar_id) {
		this.borrowCar_id = borrowCar_id;
	}

	public String getBorrowcar_modelName() {
		return borrowcar_modelName;
	}

	public void setBorrowcar_modelName(String borrowcar_modelName) {
		this.borrowcar_modelName = borrowcar_modelName;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public User getUser() {
		return user;
	}

	Object onActivate(){
		if(!userExists) return Login.class;
		else
		return null;
	}
	
	
	@SetupRender
	public void createObject(){
		chosenBorrow = (BorrowI) AppModule.context.getBean("borrow");
	}
	
	@Persist
	@Property
	private String name;
	
	@Persist
	private Date dateOfBorrow;
	
	@Persist
	private Date returnDateOfBorrow;
	
	@Inject
	private CarsI cars;
	

	@Persist
	@Property
	private double sumarry;
	
	@Inject
	private Users users;
	
	@Inject
	private BorrowsI borrows;
	
	@InjectPage
	private BrowseBorrow browseBorrowPage;
	
	@Component(id="borrowInputForm")
	private Form borrowInputForm;
	
	@Inject
	private Messages messages;
	
	public Date getDateOfBorrow() {
			return dateOfBorrow;
	}

	public void setDateOfBorrow(Date dateOfBorrow) {
			this.dateOfBorrow = dateOfBorrow;
	}
	
	

	public Date getReturnDateOfBorrow() {
			return returnDateOfBorrow;		
	}

	public void setReturnDateOfBorrow(Date returnDateOfBorrow) {
		this.returnDateOfBorrow = returnDateOfBorrow;
	}

	public double sum(){
		
		sumarry = price *( (returnDateOfBorrow.getTime() - dateOfBorrow.getTime())/ (1000 * 60 * 60 * 24));
		return sumarry;
	}
	void onValidateForm(){
		if(borrowCar_id == 0 || borrowcar_modelName == null)
			borrowInputForm.recordError(messages.get("error !! you didn't selected car which you want to rent"));
		if(dateOfBorrow.after(returnDateOfBorrow) || returnDateOfBorrow.before(dateOfBorrow))
			borrowInputForm.recordError(messages.get("wrong input with dates of borrows"));
		if(dateOfBorrow.equals(returnDateOfBorrow))
			borrowInputForm.recordError(messages.get("you can't rent a car with same dates of borrow and restore date of borrow"));
	}
	
	
	Object onSuccess(){
		CarI carBean = new CarBean();
		User userBean = new UserBean();
		
		userBean =  users.retriveSingleUser(getUser().getId());
		carBean = cars.retriveSingleCar(getBorrowCar_id());
		BorrowI borrowBean = new BorrowBean();
		
		borrowBean.executeBorrowCreation(name, dateOfBorrow,returnDateOfBorrow ,sum() ,userBean,carBean);
		borrows.save(borrowBean);
		borrowCar_id = 0;
		borrowcar_modelName = null;
		name ="";
		dateOfBorrow= null;
		returnDateOfBorrow = null;
		return browseBorrowPage;
	}
}
